package com.youngjun.common.mybatis.page;

import com.youngjun.common.exception.CommonExceptionType;
import com.youngjun.common.oss.expection.CommonException;

import java.util.List;

/**
 * @author virgojun
 */
public class PageInfo<T> {

    private final Pagination pagination;
    private final List<T> items;
    private final String orderBy;

    public PageInfo(List<T> items, PageParams pageParams) {
        if (items == null) {
            throw new CommonException(CommonExceptionType.DB_PAGE_FAILED);
        }
        this.items = items;
        this.pagination = new Pagination(pageParams.getPage().getTotal(), pageParams.getCurrentPage(), pageParams.getPageSize());
        this.orderBy = pageParams.getPage().getOrderBy();
    }

    public Pagination getPagination() {
        return pagination;
    }

    public List<T> getItems() {
        return items;
    }

    public String getOrderBy() {
        return orderBy;
    }

    static class Pagination {
        private final long total;
        private final long current;
        private final long pageSize;

        Pagination(long total, long current, long pageSize) {
            this.total = total;
            this.current = current;
            this.pageSize = pageSize;
        }

        public long getTotal() {
            return total;
        }

        public long getCurrent() {
            return current;
        }

        public long getPageSize() {
            return pageSize;
        }
    }
}
